﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Windows;
using System.Windows.Media;
using LiveCharts.Definitions.Points;
using LiveCharts.Definitions.Series;
using LiveCharts.Helpers;
using LiveCharts.SeriesAlgorithms;
using LiveCharts.Wpf;
using LiveCharts.Wpf.Points;

namespace LiveCharts.Wpf
{
  
    public class GCandleSeries : GearedSeries, ISeriesView, IFinancialSeriesView
    {


        public static readonly DependencyProperty MaxColumnWidthProperty = DependencyProperty.Register("MaxColumnWidth", typeof(double), typeof(GCandleSeries), new PropertyMetadata(0.0));

        public static readonly DependencyProperty IncreaseBrushProperty = DependencyProperty.Register("IncreaseBrush", typeof(Brush), typeof(GCandleSeries), new PropertyMetadata((object)null));

        public static readonly DependencyProperty DecreaseBrushProperty = DependencyProperty.Register("DecreaseBrush", typeof(Brush), typeof(GCandleSeries), new PropertyMetadata((object)null));

        public static readonly DependencyProperty ColoringRulesProperty = DependencyProperty.Register("ColoringRules", typeof(IList<FinancialColoringRule>), typeof(GCandleSeries), new PropertyMetadata(null, smethod_0));

        internal ShapesStroke[] Paths { get; set; }


        public double MaxColumnWidth
        {
            get
            {
                return (double)((DependencyObject)(object)this).GetValue(MaxColumnWidthProperty);
            }
            set
            {
                ((DependencyObject)(object)this).SetValue(MaxColumnWidthProperty, (object)value);
            }
        }

        public Brush IncreaseBrush
        {
            get
            {
                return (Brush)((DependencyObject)(object)this).GetValue(IncreaseBrushProperty);
            }
            set
            {
                ((DependencyObject)(object)this).SetValue(IncreaseBrushProperty, (object)value);
            }
        }

        public Brush DecreaseBrush
        {
            get
            {
                return (Brush)((DependencyObject)this).GetValue(DecreaseBrushProperty);
            }
            set
            {
                ((DependencyObject)this).SetValue(DecreaseBrushProperty, value);
            }
        }

        public IList<FinancialColoringRule> ColoringRules
        {
            get
            {
                return (IList<FinancialColoringRule>)this.GetValue(ColoringRulesProperty);
            }
            set
            {
                ((DependencyObject)(object)this).SetValue(ColoringRulesProperty, value);
            }
        }

        public GCandleSeries()
        {

            ((Series)this).Model = new CandleAlgorithm((ISeriesView)this);
            method_2();
        }

        public GCandleSeries(object configuration)
        {

            ((Series)this).Model = (SeriesAlgorithm)new CandleAlgorithm((ISeriesView)this);
            ((Series)this).Configuration = configuration;
            method_2();
        }

        public override void OnSeriesUpdateStart()
        {
            method_4();
        }

        public override void Erase(bool removeFromView = true)
        {
            method_3(removeFromView);
        }

        public override IChartPointView GetPointView(ChartPoint point, string label)
        {
            return method_5(point, label);
        }

        private void method_2()
        {
            SetCurrentValue(Series.StrokeThicknessProperty, 1.0);
            SetCurrentValue(MaxColumnWidthProperty, 35.0);
            SetCurrentValue(FrameworkElement.MaxWidthProperty, 25.0);
            SetCurrentValue(IncreaseBrushProperty, new SolidColorBrush(Color.FromRgb(76, 174, 80)));
            SetCurrentValue(DecreaseBrushProperty, new SolidColorBrush(Color.FromRgb(238, 83, 80)));
            Func<ChartPoint, string> value = (ChartPoint chartPoint_0) => $"O: {chartPoint_0.Open}, H: {chartPoint_0.High}, L: {chartPoint_0.Low} C: {chartPoint_0.Close}";
            SetValue(Series.LabelPointProperty, value);
            DefaultFillOpacity = 1.0;

        }

        private void method_3(bool bool_0)
        {
            LiveCharts.Helpers.Extentions.ForEach<ChartPoint>(ActualValues.GetPoints(this), chartPoint_0 =>
              {
                  if (chartPoint_0.View != null)
                  {
                      chartPoint_0.View.RemoveFromView(((Series)this).Model.Chart);
                  }
              });
            if (bool_0)
            {
                ShapesStroke[] paths = Paths;
                foreach (ShapesStroke stroke in paths)
                {
                    Model.Chart.View.RemoveFromDrawMargin(stroke.Path_0);
                }
                Model.Chart.View.RemoveFromView(this);
            }
        }

        private static void smethod_0(DependencyObject dependencyObject_0, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs_0)
        {
            GCandleSeries obj = (GCandleSeries)dependencyObject_0;
            obj.Paths = obj.ColoringRules.Select((FinancialColoringRule financialColoringRule_0) => new ShapesStroke()).ToArray();
            Series.CallChartUpdater(false)(dependencyObject_0, dependencyPropertyChangedEventArgs_0);
        }

        private void method_4()
        {
            if (ColoringRules == null)
            {
                if (Paths == null || Paths.Length == 0)
                {
                    Paths = new ShapesStroke[2]
                    {
                    new ShapesStroke(),
                    new ShapesStroke()
                    };
                }
                Paths[0].method_0(IncreaseBrush, IncreaseBrush, (Series)(object)this);
                Paths[1].method_0(DecreaseBrush, DecreaseBrush, (Series)(object)this);
                return;
            }
            for (int i = 0; i < ColoringRules.Count; i++)
            {
                FinancialColoringRule val = ColoringRules[i];
                if (Paths[i] == null)
                {
                    Paths[i] = new ShapesStroke();
                }
                Paths[i].method_0(val.Fill, val.Stroke, (Series)(object)this);
            }
        }

        private IChartPointView method_5(ChartPoint chartPoint_0, string string_0)
        {
            Class5 @class = (Class5)chartPoint_0.View;
            if (@class == null)
            {
                Class5 class2 = new Class5();
                class2.IsNew = true;
                class2.method_1(new Class2());
                @class = class2;
            }
            else
            {
               @class.IsNew = false;
                chartPoint_0.SeriesView.Model.Chart.View.EnsureElementBelongsToCurrentDrawMargin(@class.DataLabel);
            }
            if (DataLabels)
            {
                ((PointView)@class).DataLabel = ((Series)this).UpdateLabelContent(new DataLabelViewModel
                {
                    FormattedText = string_0,
                    Point = chartPoint_0
                }, ((PointView)@class).DataLabel);
            }
            if (!((Series)this).DataLabels && ((PointView)@class).DataLabel != null)
            {
                ((Series)this).Model.Chart.View.RemoveFromDrawMargin((object)((PointView)@class).DataLabel);
                ((PointView)@class).DataLabel = null;
            }
            return (IChartPointView)(object)@class;
        }

        [CompilerGenerated]
        private void method_6(ChartPoint chartPoint_0)
        {
            if (chartPoint_0.View != null)
            {
                chartPoint_0.View.RemoveFromView(((Series)this).Model.Chart);
            }
        }
    }
}
